<?php
/**
 * Created by PhpStorm.
 * User: liyan
 * Date: 2019/2/28
 * Time: 19:23
 */

namespace app\m\controller;

use think\Db;

class Verify
{


    //添加/修改身份申请
    //name 姓名
    //duty 职务
    //pid 主体id     (选择学校得到)
    //type 1 校足办，2校长，3教师
    //area  区域code (校足办选择省市区得到)
    //sms_code 验证码
    public function add_role_apply()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录信息失效"}';
            return;
        }
        $oid = $ses['oid'];
        $uid = $ses['uid'];

        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $name = trim($_GET['name']);
        $duty = isset($_GET['duty']) ? addslashes(trim($_GET['duty'])) : '';
        $pid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
        $type = intval($_GET['type']);
        $area = isset($_GET['area']) ? intval($_GET['area']) : 0;
        if (!$name) {
            echo '{"err":1001,"msg":"param error"}';
            return;
        }
        if ($type == 1) {
            //校足办认证，根据区域code获取校足办id或者添加校足办
            $qs = Db::query('select id from x1_unit where area=?', [$area]);
            if (!$qs) {
                $qs = Db::query('select name,shortname from area where code=?', [$area]);
                if (!$qs) {
                    echo '{"err": "1001","msg": "没有查到此区域"}';
                    return;
                }
                $unit = $qs[0]['name'];
                $shortname = $qs[0]['shortname'];
                $unit_type = 1;
                if ($area % 10000) {
                    $unit_type = 3;
                } elseif ($area % 100) {
                    $unit_type = 2;
                }
                $data = ['name' => $unit . '校园足球工作领导小组', 'shortname' => $shortname . '校足办', 'area' => $area, 'address' => '', 'type' => $unit_type];
                $pid = Db::table('x1_unit')->insertGetId($data);
            } else {
                $pid = $qs[0]['id'];
            }
        } else {
            //校长或教师认证
            if ($type == 2 && !$duty) {
                $duty = '校长';
            }
            $qs = Db::query('select area from x1_school where id=?', [$pid]);
            if (!$qs) {
                echo '{"err":1002,"msg":"该学校不存在"}';
                return;
            }
            $area = $qs[0]['area'];
        }
        $now = $_SERVER['REQUEST_TIME'];
        $data = ['uid' => $uid, 'pid' => $pid, 'type' => $type, 'name' => $name, 'duty' => $duty, 'area' => $area, 'msg' => '', 'result' => 0, 'time' => $now, 'ack_id' => '', 'ack_time' => 0];
        if ($id) {
            Db::table('x1_role_apply')->where('id', $id)->update($data);
        } else {
            $id = Db::table('x1_role_apply')->insertGetId($data);
        }
        $user = Db::query('select name from user where uid=?', [$uid]);
        if ($user && !$user[0]['name']) {
            Db::execute('update user set name=? where uid=?', [$name, $uid]);
        }
        echo json_encode(['err' => 0, 'msg' => '申请成功，正在等待审核中', 'id' => $id]);
        return;
    }


    //用户身份验证申请列表
    //type   0 未审核，1 已审核
    public function role_apply_list()
    {
        $type = isset($_GET['type']) ? intval($_GET['type']) : 0;
        $m = isset($_GET['m']) ? intval($_GET['m']) : 0;
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"身份信息失效"}';
            return;
        }
        $uid = $ses['uid'];
        $qs = Db::query('select r.uid,r.pid,r.type,r.duty,n.area,n.type unit_type,area.pc from x1_user_role r left join x1_unit n on r.pid=n.id left join area on n.area=area.code where r.uid=? and r.state=1', [$uid]);
        $users = $qs;
        $where = '';
        $where1 = [];
        $order = '';
        $list = [];
        $is_check = 0;
        foreach ($users as $v) {
            if ($v['type'] <= 2) {
                $is_check = 1;
                if ($v['type'] == 1 && $v['unit_type'] == 1) {
                    $where1[] = "(a.type=1 and (area.pc='{$v['area']}' or a.area='{$v['area']}'))";
                } elseif ($v['type'] == 1 && $v['unit_type'] == 2) {
                    $where1[] = "(a.type=1 and area.pc='{$v['area']}')";
                } elseif ($v['type'] == 1 && $v['unit_type'] == 3) {
                    $where1[] = "(a.type=2 and a.area='{$v['area']}')";
                } elseif ($v['type'] == 2) {
                    $where1[] = "(a.type=3 and a.pid='{$v['pid']}')";
                }
            }
        }
        if ($is_check == 1) {
            if ($type) {
                //已审核，时间正序排列
                $where = 'a.result in (2,3) and ';
                $order = ' order by a.id';
                if ($m && $id) {
                    $where = "a.id > $id and ";
                }
            } else {
                //未审核，时间倒序排列
                $where = 'a.result = 0 and ';
                $order = ' order by a.id desc';
                if ($m && $id) {
                    $where = "a.id < $id and ";
                }
            }
            $list = Db::query('select a.id,a.pid,a.uid,a.name,a.duty,a.result,a.msg,a.time,a.type,a.area,s.name school_name,n.name unit_name,u.avatar from x1_role_apply a left join area on a.area=area.code left join x1_school s on a.pid=s.id left join x1_unit n on a.pid=n.id left join user u on a.uid=u.uid where ' . $where . '(' . join(' or ', $where1) . ')' . $order . ' limit 0,20');
        }
        $check = [0 => '未审核', '2' => '审核通过', 3 => '已拒绝', 6 => '申请已撤回'];
        foreach ($list as $k => $v) {
            $list[$k]['time'] = date('m-d H:i', $v['time']);
            $list[$k]['check'] = $check[$v['result']];
            $list[$k]['pname'] = $list[$k]['type'] == 1 ? $list[$k]['unit_name'] : $list[$k]['school_name'];
            $list[$k]['avatar'] = $v['avatar'] ? $v['avatar'] : 'https://c.g56.cn/x9/user_im.png';
            unset($list[$k]['school_name']);
            unset($list[$k]['unit_name']);
        }
        echo json_encode(["err" => 0, "msg" => "获取成功", "list" => $list]);
    }

    //审核用户身份申请详情
    public function role_apply_detail()
    {
        $user = session('ses');
        if (!$user) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $uid = $user['uid'];
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if ($id) {
            $qs = Db::query('select a.id,a.name,a.duty,a.pid,a.type,a.area,a.result,a.msg,a.time,u.phone,s.name school_name,n.name unit_name from x1_role_apply a left join x1_school s on a.pid=s.id left join x1_unit n on a.pid=n.id left join user u on a.uid=u.uid  where a.id=?', [$id]);
        } else {
            $qs = Db::query('select a.id,a.name,a.duty,a.pid,a.type,a.area,a.result,a.msg,a.time,u.phone,s.name school_name,n.name unit_name from x1_role_apply a left join x1_school s on a.pid=s.id left join x1_unit n on a.pid=n.id left join user u on a.uid=u.uid  where a.uid=?', [$uid]);
        }

        if (!$qs) {
            echo json_encode(['err' => 0, 'msg' => '未申请', 'apply' => []]);
            return;
        }
        $apply = $qs[0];
        $list = [];
        $apply['pname'] = $apply['type'] == 1 ? $apply['unit_name'] : $apply['school_name'];
        $apply['time'] = date('m-d H:i', $apply['time']);
        unset($apply['unit_name']);
        unset($apply['school_name']);
        echo json_encode(['err' => 0, 'msg' => '已申请', 'apply' => $apply]);
    }

    //keywords  关键字
    //longitude 经度
    //latitude  纬度
    public function search_school()
    {
        $user = session('ses');
        if (!$user) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $uid = $user['uid'];

        $keywords = isset($_GET['keywords']) ? addslashes($_GET['keywords']) : '';
        $lng = isset($_GET['lng']) ? $_GET['lng'] : '';
        $lat = isset($_GET['lat']) ? $_GET['lat'] : '';

        if ($keywords) {
            $list = Db::table('x1_school')->where('name', 'like', ':name')->bind(['name' => "%{$keywords}%"])->limit(0, 100)->select();
        } else {
            //通过经纬度查询2千米以内的
            $distance = 2;  //距自己距离，千米
            $earth_radius = 6371; //地球半径，千米
            $dlng = 2 * asin(sin($distance / (2 * $earth_radius)) / cos(deg2rad($lat)));
            $dlng = rad2deg($dlng);
            $dlat = $distance / $earth_radius;
            $dlat = rad2deg($dlat);

            $max_lat = $lat + $dlat;
            $min_lat = $lat - $dlat;
            $max_lng = $lng + $dlng;
            $min_lng = $lng - $dlng;

            $qs = Db::query('select id,name,address,lat,lng from x1_school where lat>? and lat<? and lng>? and lng<? limit 0,20', [$min_lat, $max_lat, $min_lng, $max_lng]);

            $list = $qs;

        }

        echo json_encode(['err' => 0, 'msg' => '获取学校成功', 'list' => $list]);
    }


    public function search_pos()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }

        $lng = isset($_GET['longitude']) ? floatval($_GET['longitude']) : 0;
        $lat = isset($_GET['latitude']) ? floatval($_GET['latitude']) : 0;

        $pos = $lat . ',' . $lng;
        $loc = $this->tx_addr($pos);
        //通过经纬度查询2千米以内的
        $distance = 2;  //距自己距离，千米
        $earth_radius = 6371; //地球半径，千米

        $dlng = 2 * asin(sin($distance / (2 * $earth_radius)) / cos(deg2rad($lat)));
        $dlng = rad2deg($dlng);
        $dlat = $distance / $earth_radius;
        $dlat = rad2deg($dlat);

        $max_lat = $lat + $dlat;
        $min_lat = $lat - $dlat;
        $max_lng = $lng + $dlng;
        $min_lng = $lng - $dlng;

        $qs = Db::query('select id,name,address,lat,lng from x1_school where lat>? and lat<? and lng>? and lng<? limit 0,20', [$min_lat, $max_lat, $min_lng, $max_lng]);


        $list = $qs;

        echo json_encode(['err' => 0, 'msg' => '获取成功', 'pos' => $loc, 'list' => $list]);

    }

    /**
     * 列出省 + 城市列表  省级显示省级校足办
     */
    public function city_list()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $uid = $ses['uid'];
        // 列出市级
        $list = Db::query("select code,shortname name from area where pc='41'");
        array_unshift($list, ['code' => '4100', 'name' => '河南省']);
        $pu = ['code' => '4100', 'name' => '省校足办'];
        $area = [$list, [$pu]];
        // 个人信息

        echo json_encode(['err' => 0, 'msg' => 'ok', 'areas' => $area]);
    }

    //搜索地区
    public function area_srh()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $list = [];
        // $uid = $ses['uid'];
        $pc = $_GET['area'];

        if (substr($pc, 2, 2) != '00') {
            $qs = Db::query('select code,shortname as name from area where pc=?', [$pc]);
            foreach ($qs as $a) {
                if ($a['name'] == '市辖区') {
                    $a['name'] = '市校足办';
                }
                $list[] = $a;
            }
        } else {
            $list[] = ['code' => '4100', 'name' => '省校足办'];
        }
        // array_unshift($list, ['code' => $pc, 'name' => '市校足办']);
        echo json_encode(['err' => 0, 'msg' => 'ok', 'list' => $list]);
    }

    public function unit_srh()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $area = $_GET['area'];

        $area = '41'; // 校足办代码

        $qs = Db::query('select shortname name from x1_unit where area=?  limit 0,1', [$area]);
        if (!$qs) {
            echo '{"err":101,"msg":"找不到该区域校足办"}';
            return;
        }
        $rev = ['err' => 0, 'msg' => 'ok', 'area' => $area, 'name' => $qs[0]['name']];
        echo json_encode($rev);
    }

    private function verify_code()
    {
        $redis = redis();
        $inc = rand(30, 99);
        return '191' . $redis->incrBy('x1_vcode', $inc);
    }

    public function verify_code_init()
    {
        $redis = redis();
        $key = 'x1_vcode';
        $code = $redis->get($key);
        if ($code == false) {
            $redis->setnx($key, 1267819);
        }
    }

    /**
     * 申请认证
     */
    public function unit_ask()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录信息失效"}';
            return;
        }
        $uid = $ses['uid'];
        $name = $_GET['name'];
        $duty = $_GET['duty'];
        $area = $_GET['area'];

        //校足办认证，根据区域code获取校足办id或者添加校足办
        $qs = Db::query('select id,area,pc from x1_unit where area=?', [$area]);
        if (!$qs) {
            echo '{"err":101,"msg":"所在区域校足办不存在"}';
            return;
        }
        $unit = $qs[0];
        $code = $this->verify_code();

        $now = $_SERVER['REQUEST_TIME'];
        $pid = $unit['id'];

        // type 1:校足办 2:校长 3:老师

        $data = ['code' => $code, 'uid' => $uid, 'pid' => $pid, 'type' => 1, 'name' => $name, 'duty' => $duty, 'msg' => '', 'result' => 0, 'time' => $now, 'ack_id' => '', 'ack_time' => 0];

        $id = Db::table('x1_verify')->insertGetId($data);

        /*
        $user = Db::query('select name from user where uid=?', [$uid]);
        if ($user && !$user[0]['name']) {
            // Db::execute('update user set name=? where uid=?', [$name, $uid]);
        }
        */

        echo json_encode(['err' => 0, 'msg' => '认证已提交审核...', 'id' => $id]);
        return;
    }


    //审核用户申请  审核当前申请，发送通知，成功时修改用户角色，获取下一申请id
    //id  role_apply表id
    //result 2通过，3拒绝
    //msg 留言信息
    public function check_role_apply()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $uid = $ses['uid'];
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $result = intval($_GET['result']);
        $msg = isset($_GET['msg']) ? addslashes($_GET['msg']) : '';
        $now = $_SERVER['REQUEST_TIME'];
        if (!$id) {
            echo '{"err":1001,"msg":"param error"}';
            return;
        }
        $qs = Db::query('select a.id,a.uid,a.pid,a.type,a.duty,a.result,a.msg,a.area,a.name,s.name school_name,n.name unit_name from x1_role_apply a left join x1_school s on a.pid=s.id left join x1_unit n on a.pid=n.id where a.id=?', [$id]);
        if (!$qs) {
            echo '{"err":1002,"msg":"申请信息不存在"}';
            return;
        }
        $apply = $qs[0];

        $user = Db::query('select r.pid,r.type,r.duty,n.area,n.type unit_type from user u left join x1_user_role r on u.uid=r.uid left join x1_unit n on r.pid=n.id  where u.uid=? and r.state=1 ', [$uid])[0];
        if ($user['type'] > 2) {
            echo '{"err":1004,"msg":"您没有审核权限"}';
            return;
        }
        //1.更新申请信息和用户信息
        if (!is_dir(ROOT_PATH . 'm/lock')) mkdir(ROOT_PATH . 'm/lock');
        $file = ROOT_PATH . 'm/lock/a_' . $apply['id'];
        $fp = fopen($file, 'a+');
        if (flock($fp, LOCK_UN)) {
            Db::execute('update x1_role_apply set result=?,msg=?,ack_id=?,ack_time=? where id=?', [$result, $msg, $uid, $now, $id]);
            if ($result == 2) {
                Db::execute('update user set name=? where uid=?', [$apply['name'], $id]);
                if (!Db::query('select id from x1_user_role where uid=? and pid=? and type=? and state=1', [$apply['uid'], $apply['pid'], $apply['type']])) {
                    $data = ['type' => $apply['type'], 'pid' => $apply['pid'], 'uid' => $apply['uid'], 'state' => 1, 'duty' => $apply['duty'], 'time' => $now];
                    Db::table('x1_user_role')->insertGetId($data);
                }
                $behave = '';
            } else {
                $behave = '';
            }
            //2.修改或添加消息
            $qs = Db::query('select id from x1_msg_room where uid=? and muid=?', [0, $apply['uid']]);
            if ($qs) {
                $room = $qs[0];
                $room_id = $room['id'];
            } else {
                //添加msg_room数据
                $data = ['uid' => 0, 'muid' => $apply['uid'], 'state' => 1, 'mstate' => 1, 'time' => $now, 'last_msg' => ''];
                $room_id = Db::table('x1_msg_room')->insertGetId($data);
            }
            $result_txt = $result == 2 ? '通过' : '拒绝';
            $pname = $apply['type'] == 1 ? $apply['unit_name'] : $apply['school_name'];
            $content = '　　　您的身份认证申请“' . $pname . '的' . $apply['duty'] . '”已经' . $result_txt;
            $data = ['uid' => $apply['uid'], 'sender' => 0, 'pid' => $room_id, 'title' => '认证申请' . $result_txt . '通知', 'content' => $content, 'time' => $now, 'read_time' => '', 'behave' => $behave, 'type' => 2, 'state' => 0];
            $msg_id = Db::table('x1_msg')->insertGetId($data);
            Db::execute('update x1_msg_room set mread=mread+1,last_msg=? where id=?', [json_encode($data), $room_id]);
            //3.获取下一单未审核id
            $qs = Db::table('x1_role_apply a')->field('a.id')->join('x1_school s', 'a.pid=s.id', 'LEFT')->join('x1_unit n', 'a.pid=n.id', 'LEFT')->join('area', 'n.area=area.code')->where('a.result', 0);
            if ($user['type'] == 1 && $user['unit_type'] == 1) {
                //区取学校校长
                $qs = $qs->where('a.type', 2)->where('a.area', $user['area']);
            } elseif ($user['type'] == 1 && $user['unit_type'] == 2) {
                //市级取区级
                $qs = $qs->where('a.type', 1)->where('area.pc', $user['area']);
            } elseif ($user['type'] == 1 && $user['unit_type'] == 3) {
                //省级取市级
                $qs = $qs->where('a.type', 1)->where("(a.area='{$user['area']}' or area.pc='{$user['area']}')");
            } elseif ($user['type'] == 2) {
                //校长取教师
                $qs = $qs->where('a.type', 3)->where('a.pid', $user['pid']);
            }
            $qs = $qs->order('a.time')->limit(0, 1)->select();
            if ($qs) {
                $next_id = $qs[0]['id'];
            } else {
                $next_id = 0;
            }
            echo '{"err":0,"msg":"审核成功","next_id":' . $next_id . '}';
            return;
        }
        echo '{"err":1005,"msg":"有其他人正在处理此程序"}';
    }


    /**
     *  查询我的身份认证
     */
    public function my_role_list()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"auth error"}';
            return;
        }
        $uid = $ses['uid'];
        $rev = ['err' => 0, 'msg' => ''];
        $redis = redis();
        // 用户信息
        $qs = Db::query('select name,phone from user where uid=? limit 0,1', [$uid]);
        $user = $qs[0];

        $rs = [];
        // 查询已经验成功
        $qs = Db::query('select * from x1_user_role where uid=? order by type', [$uid]);
        if ($qs) {

            foreach ($qs as $r) {
                $type = $r['type'];
                $pid = $r['pid'];
                $r['name'] = $user['name'];
                $r['rt'] = 1;
                if ($type == 1) { // 校足办
                    $unit = igbinary_unserialize($redis->get('U' . $pid));
                    $r['unit'] = $unit['name'];
                } else { //  2/3 学校
                    $unit = igbinary_unserialize($redis->get('S' . $pid));
                    $r['unit'] = $unit['name'];
                }
                $st = $r['state'];
                if ($st == 1) {
                    $r['state'] = '已认证';
                    $r['s'] = 's3';
                }
                $rs[] = $r;
            }
        }
        // 正在认证的信息
        $qs = Db::query('select id,type,pid,code,name,duty,result,time from x1_verify where uid=? and result<3', [$uid]);
        foreach ($qs as $r) {
            $type = $r['type'];
            $pid = $r['pid'];
            $r['rt'] = 2;
            if ($type == 1) {

                $unit = igbinary_unserialize($redis->get('U' . $pid));
                $r['unit'] = $unit['name'];

            } else { //  2/3 学校
                $unit = igbinary_unserialize($redis->get('S' . $pid));
                $r['unit'] = $unit['name'];
            }
            $r['time'] = date('Y-m-d H:i:s', $r['time']);
            $st = $r['result'];
            if ($st == 0) {
                $r['state'] = '待审核';
                $r['s'] = 's2';
            }
            $rs[] = $r;
        }
        $rev['rs'] = $rs;
        echo json_encode($rev);
    }

    public function item()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"auth error"}';
            return;
        }
        $uid = $ses['uid'];
        list($type, $id) = explode('_', $_GET['id']);
        if ($type == 1) {


        }


    }

    public function my_apply_detail()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $uid = $ses['uid'];
        $type = isset($_GET['type']) ? intval($_GET['type']) : 0;
        $qs = Db::query('select phone from user where uid=?', [$uid]);
        $phone = $qs ? $qs[0]['phone'] : '';
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $apply = [];
        if ($id) {
            $qs = Db::table('x1_role_apply a')->field('a.id,a.pid,a.name,a.duty,a.type,a.area,a.result,a.msg,a.time,s.name school_name,u.name unit_name')->join('x1_school s', 'a.pid=s.id', 'LEFT')->join('x1_unit u', 'a.pid=u.id', 'LEFT')->where('uid', $uid)->where('a.id', $id)->select();
            if (!$qs) {
                echo '{"err":1001,"msg":"申请信息不存在"}';
                return;
            }
            $apply = $qs[0];
            $apply['pname'] = $apply['type'] == 1 ? $apply['unit_name'] : $apply['school_name'];
            $apply['time'] = date('m-d H:i', $apply['time']);
            $apply['phone'] = $phone;
            unset($apply['unit_name']);
            unset($apply['school_name']);
            if ($apply['type'] == 1) {
                $qs = Db::query('select shortname name,pc from area where code=?', [$apply['area']]);
                $name = $qs ? $qs[0]['name'] : '';
                if ($qs && $qs[0]['pc']) {
                    $qs = Db::query('select shortname name,pc from area where code=?', [$qs[0]['pc'] * 100]);
                    $name = $qs ? $qs[0]['name'] . ' ' . $name : $name;
                    if ($qs && $qs[0]['pc']) {
                        $qs = Db::query('select shortname name,pc from area where code=?', [$qs[0]['pc'] * 100]);
                        $name = $qs ? $qs[0]['name'] . ' ' . $name : $name;
                    }
                }
                $apply['area_name'] = $name;
            }
        }
        echo json_encode(['err' => 0, 'apply' => $apply]);
    }

    //修改身份申请信息，暂时不开通
    public function edit_role_apply()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $uid = $ses['uid'];
        $id = $_GET['id'];
        $name = trim($_GET['name']);
        $duty = isset($_GET['duty']) ? addslashes(trim($_GET['duty'])) : '';
        $pid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
        $area = isset($_GET['area']) ? intval($_GET['area']) : 0;

        if (!$name) {
            echo '{"err":1001,"msg":"param error"}';
            return;
        }
        $qs = Db::query('select result,type from x1_role_apply where id=?', [$id]);
        if (!$qs) {
            echo '{"err":1002,"msg":"申请不存在"}';
            return;
        }
        $apply = $qs[0];
        $type = $apply['type'];
        if ($apply['result'] > 0) {
            echo '{"err":1003,"msg":"申请已处理，无法修改"}';
            return;
        }
        if ($type == 1) {
            //校足办认证，根据区域code获取校足办id或者添加校足办
            if (!$pid) {
                $qs = Db::query('select id from x1_unit where area=?', [$area]);
                if (!$qs) {
                    $qs = Db::query('select name,shortname from area where code=?', [$area]);
                    if (!$qs) {
                        echo '{"err": "1001","msg": "没有查到此区域"}';
                        return;
                    }
                    $unit = $qs[0]['name'];
                    $shortname = $qs[0]['shortname'];
                    $unit_type = 1;
                    if ($area % 10000) {
                        $unit_type = 3;
                    } elseif ($area % 100) {
                        $unit_type = 2;
                    }
                    $data = ['name' => $unit . '校园足球工作领导小组', 'shortname' => $shortname . '校足办', 'area' => $area, 'address' => '', 'type' => $unit_type];
                    $pid = Db::table('x1_unit')->insertGetId($data);
                } else {
                    $pid = $qs[0]['id'];
                }
            }

        } else {
            //校长或教师认证
            if ($type == 2 && !$duty) {
                $duty = '校长';
            }
            $qs = Db::query('select area from x1_school where id=?', [$pid]);
            if (!$qs) {

                echo '{"err":1002,"msg":"该学校不存在"}';
                return;
            }
            $area = $qs[0]['area'];
        }
        $now = $_SERVER['REQUEST_TIME'];
        $data = ['pid' => $pid, 'name' => $name, 'duty' => $duty, 'area' => $area, 'msg' => '', 'result' => 0, 'time' => $now, 'ack_id' => '', 'ack_time' => 0];
        $id = Db::table('x1_role_apply')->where('id', $id)->update($data);
        echo '{"err":0,"msg":"申请修改成功"}';
    }

    public function revoke_role_apply()
    {
        $ses = session('ses');
        if (!$ses) {
            echo '{"err":1,"msg":"登录状态失效"}';
            return;
        }
        $uid = $ses['uid'];
        $id = $_GET['id'];
        $qs = Db::query('select uid,result from x1_role_apply where id=?', [$id]);
        if (!$qs) {
            echo '{"err":1001,"msg":"申请信息不存在"}';
            return;
        }
        $apply = $qs[0];
        if ($apply['uid'] != $uid) {
            echo '{"err":1002,"msg":"您不是该申请的提交人，不能修改该申请"}';
            return;
        }
        if (in_array($apply['result'], [2, 3])) {
            echo '{"err":1003,"msg":"该申请已经被审核，无法撤回"}';
            return;
        }
        if ($apply['result'] == 6) {
            echo '{"err":1004,"msg":"该申请已经撤回"}';
            return;
        }
        Db::query('update x1_role_apply set result=? where id=?', [6, $id]);
        echo '{"err":0,"msg":"申请撤回成功"}';
        return;
    }


    protected function tx_addr($loc)
    {
        $str = curl('https://apis.map.qq.com/ws/geocoder/v1/?key=BCJBZ-ZDIW4-R7QUD-XVMDH-JCKXE-VFBYX&output=json&poi_options=address_format=short;radius=1000;policy=4&location=' . $loc);
        // loger($str, 'kong');
        $rev = json_decode($str, true);
        if ($rev['status'] == 0) {
            return $rev['result'];
        } else {
            return null;
        }
    }


}